<template>
  <div class="index">
    <article-list
      :article="article"
      @loadmore="loadmoreArticle"
    />
  </div>
</template>

<script>
import ArticleList from '~/components/archive/list'

export default {
  name: 'TagArticleList',
  validate({ params, store }) {
    return params.tag_slug && store.state.tag.data.some((tag, index, arr) => {
      return Object.is(tag.slug, params.tag_slug)
    })
  },
  components: {
    ArticleList
  },
  computed: {
    article() {
      return this.$store.state.article.list
    },
    currentTag() {
      return this.$store.state.tag.data.find((tag, index, arr) => {
        return Object.is(tag.slug, this.$route.params.tag_slug)
      })
    },
    defaultParams() {
      return {
        tag_slug: this.$route.params.tag_slug
      }
    },
    nextPageParams() {
      const { current } = this.article.data.pagination
      return Object.assign({
        page: Number(current) + Number(1)
      }, this.defaultParams)
    }
  },
  fetch({ store, params }) {
    return store.dispatch('article/fetchList', params)
  },
  head() {
    const slug = this.defaultParams.tag_slug || ''
    const title = slug.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase())
    return { title: `${title} | Tag` }
  },
  created() {
    if (!this.currentTag) {
      this.$router.back()
    }
  },
  methods: {
    loadmoreArticle() {
      this.$store.dispatch('article/fetchList', this.nextPageParams)
    }
  }
}
</script>
